﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

public partial class User_account : System.Web.UI.Page
{
    public Cstudy.MODEL.users user = new Cstudy.MODEL.users();

    public string filePath
    {
        get { return ConfigurationManager.AppSettings["filePath"]; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            
        }
        if (GetLoginStates())
        {
            LoadUser();
            Title = user.UserName + "-个人中心";
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
        
    }

    protected bool GetLoginStates()
    {
        
        if (Request.Cookies["user"] != null)
        {
            HttpCookie cookies = Request.Cookies["user"];
            user.UserID = int.Parse(cookies.Values["userid"]);
            user.UserName = cookies.Values["name"];
            return true;
        }
        return false;
    }

    protected void LoadUser()
    {
        DataSet ds = new DataSet();
        ds = new Cstudy.BLL.Users().SelectUserByID(user.UserID);
        DataRow dr = ds.Tables[0].Rows[0];
        user.Email = dr["Email"].ToString();
        user.Pwd = dr["Password"].ToString();
        user.UserPower = int.Parse(dr["UserPower"].ToString());
        user.NickName = dr["NickName"].ToString();
        user.Sex = (Cstudy.MODEL.users.UserSex)dr["Sex"];
        user.Telephone = dr["Telephone"].ToString();
        user.Birth = dr["Birth"].ToString();
        user.Headimage = dr["Headimage"].ToString();
        user.Info = dr["Info"].ToString();
        user.UserScore = (int)dr["UserScore"];
        user.LastLogin = (DateTime)dr["LastLogin"];
    }
    protected void UserMenu_MenuItemClick(object sender, MenuEventArgs e)
    {
        multiView.ActiveViewIndex = int.Parse(e.Item.Value);
        if(multiView.ActiveViewIndex==3)
        {
            LoadInfo();
        }
    }
    protected void Uploadbtn_Click(object sender, EventArgs e)
    {
        if (FileUpLoad1.HasFile)
        {

            if (FileUpLoad1.PostedFile.ContentLength < 1048576)
            {
                try
                {
                    string suf = FileUpLoad1.FileName.Substring(FileUpLoad1.FileName.LastIndexOf("."), FileUpLoad1.FileName.Length - FileUpLoad1.FileName.LastIndexOf("."));
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssff");
                    string filepath = Server.MapPath("~/upload/heads/");
                    string filedir = DateTime.Now.ToString("yyyy/MM/");
                    if (!System.IO.Directory.Exists(filepath + filedir))
                    {
                        System.IO.Directory.CreateDirectory(filepath + filedir);
                    }
                    lblMessage.Text = filepath + filename + suf;
                    FileUpLoad1.PostedFile.SaveAs(filepath + filedir + filename + suf);
                    string oldimage = user.Headimage;
                    user.Headimage = filedir + filename + suf;
                    lblMessage.Text = "上传成功!";
                    if (oldimage.Length > 0)
                    {
                        try
                        {
                            if (System.IO.File.Exists(filepath + oldimage))
                            {
                                System.IO.File.Delete(filepath + oldimage);
                            }
                        }
                        catch { }
                    }
                    new Cstudy.BLL.Users().UpdateUser(user);
                }
                catch (Exception ex)
                {
                    //lblMessage.Text = "出现异常,无法上传!";
                    lblMessage.Text += ex.Message;
                }

            }
            else
            {
                lblMessage.Text = "上传文件不能大于1MB!";
            }
        }
        else
        {
            lblMessage.Text = "尚未选择文件!";
        }  
    }

    protected void LoadInfo()
    {
        LoadUser();
        nickNameBox.Text = user.NickName;
        DropDownList1.SelectedIndex = (int)user.Sex;
        phoneBox.Text = user.Telephone;
        birthBox.Text = user.Birth;
        infoBox.Text = user.Info;
    }
    protected bool extixnewpwd()
    {
        string pwd = oldpwdBox.Text;
        string oldpwd = new Cstudy.Tools.Tools().MakeMD5(oldpwdBox.Text);
        if (oldpwd == user.Pwd)
        {
            return true;
        }
        else
        {
            oldlit.Text = "密码错误";
            return false;
        }
    }
    protected bool extisoldpwd()
    {
        if (new Cstudy.Tools.RegexTools().IsValidByte(newpwdBox.Text, 5, 15))
        {
            return true;
        }
        else
        {
            newlit.Text = "密码格式错误";
            return false;
        }
    }

    protected void changepwdbtn_Click(object sender, EventArgs e)
    {
        if (extisoldpwd() & extixnewpwd())
        {
            user.Pwd = new Cstudy.Tools.Tools().MakeMD5(newpwdBox.Text);
            if (new Cstudy.BLL.Users().UpdateUser(user))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "succss", "alert('修改成功');", true);
                LoadUser();
            }
        }
    }
    protected void Savebtn_Click(object sender, EventArgs e)
    {
        if (new Cstudy.Tools.RegexTools().IsValidTel(phoneBox.Text.Trim()))
        {
            user.NickName = nickNameBox.Text;
            user.Sex = (Cstudy.MODEL.users.UserSex)DropDownList1.SelectedIndex;
            user.Telephone = phoneBox.Text;
            user.Birth = birthBox.Text;
            user.Info = infoBox.Text;
            if (new Cstudy.BLL.Users().UpdateUser(user))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "succss", "alert('修改成功');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "fail", "alert('修改失败');", true);
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "phonefail", "alert('手机号码不正确');", true);
        }
    }
}